package br.ucb.rest.negocio.impl;

import javax.persistence.EntityManager;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import br.ucb.rest.negocio.AbstractBusinessObject;
import br.ucb.rest.persistencia.dao.GenericDAO;
import br.ucb.rest.persistencia.vo.MesaVO;

public class MesaBOS extends AbstractBusinessObject {
	private static MesaBOS instance;
	private Logger logger = Logger.getLogger(MesaBOS.class);
	
	private MesaBOS() {
		super();
		logger.info("Singleton instanciado com sucesso!");
	}
	public static MesaBOS getInstance() {
		if (instance == null) {
			instance = new MesaBOS();
		}
		return instance;
	}
	
	public MesaVO recuperarPorNumeroMesa(Integer nuMesa) {
		EntityManager em = emf.createEntityManager();
		GenericDAO dao = GenericDAO.getInstance(em);
		try {
			
			DetachedCriteria criteria = DetachedCriteria.forClass(MesaVO.class);
			criteria.add(Restrictions.eq("numeroMesa", nuMesa));
			
			return (MesaVO) dao.criteriaResultadoUnico(criteria);
			
		}catch (Exception e) {
			logger.error("Ocorreu um erro ao buscar registros no banco de dados!", e);
			return null;
		}finally {
			GenericDAO.liberarInstancia();
			em.close();
		}
		
	}
	

}
